﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CollegeShop.Infrastructure.Component.Cache
{
    public class iCache
    {
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="loadAction"></param>
        /// <param name="Duration">单位(s)</param>
        /// <returns></returns>
        public static T Get<T>(string key, Func<T> loadAction, int duration)
            where T : class
        {
            T data = CacheHelper.GetCache<T>(key);
            if (data == null)
            {
                data = loadAction.Invoke();
                if (data != null)
                {
                    if (duration > 0)
                    {
                        return CacheHelper.Add<T>(key, data, CacheExpireStrategy.Absolute, new TimeSpan(0, 0, duration));
                    }
                    else
                    { 
                        //永不过期
                        return CacheHelper.Add(key, data) as T;
                    }
                }
            }
            return data;
        }
    }
}
